from flask import Blueprint,request,make_response,jsonify,redirect
import json, random, time, requests
from utils.myredis import r
from models.models import *

serve_blue = Blueprint("serveblue",__name__)

@serve_blue.route("/qneed",methods=["POST"])
# 抢需求
def qneed():
    data = json.loads(request.data)
    jdr = data["userid"]
    code = data["code"]
    # hash取模
    number = hash(code) % 3
    if number == 0:
        res = Need.query.filter_by(code=code).first()
        if res:
            res.status = 3,
            res.jdid = jdr
            db.session.add(res)
            db.session.commit()
        return jsonify({"code": 200, "msg": "已抢"})
    else:
        needs = "Need" + str(number)
        needs = globals().get(f'{needs}')
        res = needs.query.filter_by(code=code).first()
        if res:
            res.status = 3,
            res.jdid = jdr
            db.session.add(res)
            db.session.commit()
        return jsonify({"code":200,"msg":"已抢"})















